import { Injectable } from '@nestjs/common';
import { FileSystemEntry } from '../virtual-file-system/types';
import { LcDocument, LcProject } from '../../types/editor.type';
import { ITypeEntity } from '../../../superType/types';

/**
 * FileSystemEntry 构建器服务
 * 统一维护不同资源类型的 FileSystemEntry 构建逻辑
 */
@Injectable()
export class FileSystemEntryBuilder {
  /**
   * 从 LcProject 构建 FileSystemEntry (主文件)
   */
  buildFromProject(project: LcProject): FileSystemEntry {
    return {
      path: project.path,
      content: undefined,
      stats: {
        type: 'file',
        size: 0,
        createTime: project.create_time,
        modifyTime: project.update_time,
        modifyBy: project.lastUpdateBy,
        // 通用元数据
        title: project.title,
        description: project.description,
        width: project.width,
        height: project.height,
        screenshot: project.screenshot,
        // 组件元数据
        frames: project.frames,
        // 标识字段
        isDocument: true,
        contentHash: project.hash,
      },
    };
  }

  /**
   * 从 LcProject 构建 CSS FileSystemEntry
   */
  buildCssFromProject(project: LcProject): FileSystemEntry | null {
    if (!project.cssHash || !project.cssPath) {
      return null;
    }

    return {
      path: project.cssPath,
      content: undefined,
      stats: {
        type: 'file',
        size: 0,
        createTime: project.create_time,
        modifyTime: project.cssUpdateAt,
        modifyBy: project.lastUpdateBy,
        contentHash: project.cssHash,
      },
    };
  }

  /**
   * 从 LcDocument 构建 FileSystemEntry (主文件)
   */
  buildFromDocument(doc: LcDocument): FileSystemEntry {
    return {
      path: doc.path,
      content: undefined,
      stats: {
        type: 'file',
        size: 0,
        createTime: doc.create_time,
        modifyTime: doc.update_time,
        modifyBy: doc.lastUpdateBy,
        // 通用元数据
        title: doc.title,
        description: doc.description,
        width: doc.width,
        height: doc.height,
        screenshot: doc.screenshot,
        // 组件元数据
        portal: doc.portal,
        frames: doc.frames,
        route: doc.route,
        // 标识字段
        isDocument: true,
        componentType: doc.type,
        contentHash: doc.hash,
      },
    };
  }

  /**
   * 从 LcDocument 构建 CSS FileSystemEntry
   */
  buildCssFromDocument(doc: LcDocument): FileSystemEntry | null {
    if (!doc.cssHash || !doc.cssPath) {
      return null;
    }

    return {
      path: doc.cssPath,
      content: undefined,
      stats: {
        type: 'file',
        size: 0,
        createTime: doc.create_time,
        modifyTime: doc.cssUpdateAt,
        modifyBy: doc.lastUpdateBy,
        contentHash: doc.cssHash,
      },
    };
  }

  /**
   * 从 ITypeEntity 构建 FileSystemEntry
   */
  buildFromTypeEntity(type: ITypeEntity): FileSystemEntry {
    const isFolder = type.type === 'folder';
    const path = isFolder ? type.path || `/global/${type.name}` : type.path;

    return {
      path,
      content: undefined,
      stats: {
        type: isFolder ? 'directory' : 'file',
        size: type.size || 0,
        createTime: type.create_time,
        modifyTime: type.update_time,
        modifyBy: type.userId,
        // 通用元数据
        title: type.desc,
        // 类型元数据
        routePath: type.routePath,
        noLayout: type.noLayout,
        contentHash: type.hash,
      },
    };
  }
}

